package com.liang.publics.ui;

import android.annotation.SuppressLint;
import android.app.Activity;
import android.content.Context;
import android.os.Bundle;
import android.os.Handler;
import android.os.Looper;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.FrameLayout;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.liang.publics.config.Const;
import com.liang.publics.util.SharedPreferencesUtil;
import com.liang.maogouyuyin.R;
import com.liang.maogouyuyin.SplashActivity;
import com.liang.publics.base.BaseFragment;


import com.liang.publics.util.LogUtil;


import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
import com.liang.publics.ad.ads.AsService;

public class SettingFragment extends BaseFragment implements View.OnClickListener {
    @SuppressLint("StaticFieldLeak")
    private static volatile SettingFragment instance;

    private LinearLayout tv_user_yxxx, tv_user_yhxx, tv_user_yhfk;
    private  TextView tv_app_v_text;

    @SuppressLint("StaticFieldLeak")
    public static FrameLayout mFeedContainer;
    public SettingFragment() {
        // 私有构造函数，防止外部实例化
    }

    public   SettingFragment getInstance(Context contexts,Activity activities) {
//        if (instance == null) {
//            synchronized (SettingFragment.class) {
//                if (instance == null) {
                    instance = new SettingFragment();
//                }
//            }
//        }
        return instance;
    }

    @Nullable
    @Override
    public View onCreateView(@NotNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        return inflater.inflate(getResources().getLayout(R.layout.public_setting_fragment), container, false);
    }

    @Override
    public void onViewCreated(@NotNull View view, @Nullable Bundle savedInstanceState) {

        mFeedContainer =  getView().findViewById(R.id.fl_content_3);
        super.onViewCreated(view, savedInstanceState);
        initView(view);
    }

    @Override
    public void onResume() {
        super.onResume();
        LogUtil.d(Const.TAG,"------SettingFragment---onResume");
//        if (SharedPreferencesUtil.getStatus(SharedPreferencesUtil.IF_AD)) {
//
//            AsService adservice =    new AsService();
//            adservice.initAd("feed", context,getResources().getString(R.string.feed_native_media_id), mFeedContainer  );
//
//
//            adservice.showAd("feed", activity);
////            initAd("full", context,   mFeedContainer);
////////            //插屏
////            showAd("full", activity);
////
////            if(Const.shuangInterstitialFull) {
////                initAd("full2", context,mFeedContainer);
////
////                showAd("full2", activity);
////            }
//            //包检查
//            ApplicationUtil.ifPackageName(getContext().getApplicationContext(), "com.vivo.autotest.screen_record");
//
//        }
    }

    @SuppressLint("SetTextI18n")
    private void initView(View view) {
        tv_user_yxxx = view.findViewById(R.id.tv_user_yxxx);
        tv_user_yhxx = view.findViewById(R.id.tv_user_yhxx);
        tv_user_yhfk = view.findViewById(R.id.tv_user_yhfk);
        tv_app_v_text = view.findViewById(R.id.tv_app_v_text);
        tv_app_v_text.setText("当前版本:" + SplashActivity.versionName);
        tv_user_yxxx.setOnClickListener(this);
        tv_user_yhxx.setOnClickListener(this);
        tv_user_yhfk.setOnClickListener(this);
    }

    @SuppressLint("NonConstantResourceId")
    @Override
    public void onClick(View v) {
        if (v.getId()== R.id.tv_user_yxxx){
            WebViewActivity.gotoActivity(getContext(), "隐私协议", "ystk");

        }
        else   if (v.getId()== R.id.tv_user_yhxx){
            WebViewActivity.gotoActivity(getContext(), "用户协议", "yhxy");

        }
        else   if (v.getId()== R.id.tv_user_yhfk){
            WebViewActivity.gotoActivity(getContext(), "意见反馈", "yjfk");

        }
    }

    public static  void adLoad(int position, String feedId, FrameLayout mFeedContainer, Context context, Activity activity){
        if (SharedPreferencesUtil.getStatus(SharedPreferencesUtil.IF_AD)){

            AsService adservice = new AsService();

                String feedId2 = context.getString(R.string.feed_native_media_id);
                if (feedId != null) {
                    feedId2 = feedId;
                }

                adservice.initAd(
                        "feed",
                        context,
                        feedId2,
                        mFeedContainer
                );

                adservice.showAd("feed", activity);

            //插屏

            adservice.initAd("full", context, activity.getString(R.string.full_media_info_id), null);

            Handler handler = new Handler(Looper.getMainLooper());
            handler.postDelayed(new Runnable() {
                @Override
                public void run() {
                    adservice.showAd("full", activity);
                }
            }, 1500);

            //插屏

            adservice.initAd("full2", context, activity.getString(R.string.full_media_info_2_id), null);

            Handler handler2 = new Handler(Looper.getMainLooper());
            handler2.postDelayed(new Runnable() {
                @Override
                public void run() {
                    adservice.showAd("full2", activity);
                }
            }, 1500);
        }
    }

    public static  void adLoad2(int position, String feedId, FrameLayout mFeedContainer, Context context, Activity activity){
        if (SharedPreferencesUtil.getStatus(SharedPreferencesUtil.IF_AD)){

            AsService adservice = new AsService();

            //插屏
            adservice.initAd("full2", context, activity.getString(R.string.full_media_info_id), null);

//            Handler handler2 = new Handler(Looper.getMainLooper());
//            handler2.postDelayed(new Runnable() {
//                @Override
//                public void run() {
            adservice.showAd("full2", activity);
//                }
//            }, 1500);

            //激励视频
            adservice.initAd("reward", context, activity.getString(R.string.reward_media_id), null);

            Handler handler = new Handler(Looper.getMainLooper());
            handler.postDelayed(new Runnable() {
                @Override
                public void run() {
                    adservice.showAd("reward", activity)  ;
                }
            }, 2500);
        }
    }
}
